<template>
  <div>
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="120px">
        <el-form-item label="托盘码或RFID" prop="code">
          <el-input v-model="formData.code" placeholder="请输入托盘码或RFID" size="small" style="width: 300px;"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as EntryOrderApi from '@/api/hs/entryorder';
import {throttle} from "@/utils/throttle";

export default {
  name: "EntryOrderTripartiteForm",
  components: {},
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        code: undefined,
      },
      // 表单校验
      formRules: {
        code: [{required: true, message: '托盘码或RFID不能为空', trigger: 'blur'}],
      },
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      this.title = "新增三方入库单";
    },
    /** 提交按钮 */
    async submitForm() {
      throttle(async () => {
        // 校验主表
        await this.$refs["formRef"].validate();
        this.formLoading = true;
        try {
          const data = this.formData;
          // 添加的提交
          await EntryOrderApi.createTripartiteEntryOrder(data);
          this.$modal.msgSuccess("入库成功");
          this.dialogVisible = false;
          this.$emit('success');
        } finally {
          this.formLoading = false;
        }
      }, 500)
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        code: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
